package com.qinglei.recoup.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.utils.DateTimeUtils;
import com.qinglei.recoup.system.dao.BedHealthBreathDailyDataMapper;
import com.qinglei.recoup.system.dao.BedHealthDailyStatisticsMapper;
import com.qinglei.recoup.system.dao.TBedHealthDailyStatisticsDataMapper;
import com.qinglei.recoup.system.dao.TableFeaturesMapper;
import com.qinglei.recoup.system.domain.*;
import com.qinglei.recoup.system.domain.vo.DeanRotationVo;
import com.qinglei.recoup.system.pojo.HealthVO;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.DeanDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Api(tags = "[3.0] 院长数据看板")
@RestController
@Slf4j
@RequestMapping("/deanData")
public class DeanDataController {

    @Autowired
    private DeanDataService deanDataService;

    @Autowired
    private TBedHealthDailyStatisticsDataMapper tBedHealthDailyStatisticsDataMapper;

    @Autowired
    private BedHealthDailyStatisticsMapper bedHealthDailyStatisticsMapper;

    @Autowired
    private TableFeaturesMapper tableFeaturesMapper;

    @Autowired
    private BedHealthBreathDailyDataMapper bedHealthBreathDailyDataMapper;

    @Autowired
    private BedService bedService;

    @GetMapping("/getTopList")
    @ApiOperation(value = "获取排行榜数据")
    public CommonRes<Map<String,List<TBedHealthDailyStatisticsData>>> getTopList(){
       return CommonRes.ok(deanDataService.getTopList());
    }

    @PostMapping("/getRotationTopList")
    @ApiOperation(value = "获取轮播数据")
    public CommonRes<Map<String,Object>> getRotationTopList(@RequestBody DeanRotationVo vo){
       return CommonRes.ok(deanDataService.getRotationTopList(vo));
    }


    @GetMapping("/getWholeSleep")
    @ApiOperation(value = "获取整体睡眠数据")
    @ApiImplicitParam(name="queryType",dataType="Integer",required=false,value="查询类型 0: 过去7天 1:过去30天   查询全部时不传",defaultValue="")
    public CommonRes<Map<String,Object>> getWholeSleep(Integer queryType){
       return CommonRes.ok(deanDataService.getWholeSleep(queryType));
    }

    @GetMapping("/getGeneralSituation")
    @ApiOperation(value = "员工概况+设备概况")
    public CommonRes<Map<String,Object>> getGeneralSituation(){
       return CommonRes.ok(deanDataService.getGeneralSituation());
    }

    @ApiOperation(value = "护工数据同步")
    @GetMapping("/pushDeanData")
    public String pushDeanData(String day) {
        LocalDate yesterday = LocalDate.now().minusDays(1);
        if (StringUtils.isNotEmpty(day)) {
            yesterday = DateTimeUtils.yyyy_MM_dd(day);
        }
        log.info("into pushDeanData, yesterday = {}", yesterday);
        tBedHealthDailyStatisticsDataMapper.deleteByDate(yesterday);
        List<BedHealthDailyStatistics> bedHealthDailyStatistics = bedHealthDailyStatisticsMapper.selectListByDate(yesterday);
        TBedHealthDailyStatisticsData td;
        for (BedHealthDailyStatistics b : bedHealthDailyStatistics) {
            td = new TBedHealthDailyStatisticsData();
            BeanUtils.copyProperties(b, td);
            td.setStatisticsDate(b.getStatisticsDate());
            td.setScore(Double.valueOf(b.getScore()));
            td.setCreateTime(b.getCreateTime());
            BedRadarBO bedWithSn = bedService.getBedWithSn(b.getBedId());
            if (bedWithSn != null) {
                TableFeatures tableFeatures = tableFeaturesMapper.selectDate(bedWithSn.getSnCode(), b.getStatisticsDate().toString());
                if (tableFeatures != null) {
                    td.setVitalityIndex(tableFeatures.getVitalityIndex());
                    td.setMoraleIndex(tableFeatures.getMoraleIndex());
                    td.setFallIndex(tableFeatures.getFallIndex());
                }
                BedHealthBreathDailyData bl = bedHealthBreathDailyDataMapper.selectByDate(b.getBedId(), b.getStatisticsDate());
                if (bl != null) {
                    td.setAhi(bl.getAhi());
                    td.setHi(bl.getHi());
                    td.setAi(bl.getAi());
                }

                tBedHealthDailyStatisticsDataMapper.insertData(td);

            }
        }

        return "ok";
    }

}
